<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('合伙企业')" prop="hhqyId">
            <span slot="label">
              {{ $t('合伙企业') }}
              <el-tooltip :content="$t('已被占用的合伙企业不可以选择')" placement="top">
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <el-select v-model="form.hhqyId" value="" :placeholder="$t('合伙企业')" @change="getHhqyName">
              <el-option
                v-for="(item, index) in hhqyBaseOptions"
                :key="index"
                :label="$t(item.name)"
                :value="item.id"
                :disabled="item.disabled"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('占用时间')" prop="dateInvestmentEstimate">
            <DatePicker
              v-model="form.dateInvestmentEstimate"
              :placeholder="$t('占用时间')"
            />

          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('占用原因')" prop="reason">
            <el-input v-model="form.reason" type="textarea" :placeholder="$t('占用原因')" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="hhqy:occupy" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
    import { saveHhqyOccupy, getHhqyOccupy } from '@/api/hhqy/occupy/hhqyOccupy'
    import { findNotExistsOccupy } from '@/api/hhqy/base/hhqyBase'
    export default {
      components: {

      },
        data() {
            return {
              // 菜单标识
                menuKey: 'hhqy_occupy',
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                  hhqyId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], dateInvestmentEstimate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], amountInvestmentEstimate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], reason: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], actTitle: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 未被项目占用的合伙企业
                hhqyBaseOptions: []
            }
        },
        created() {
            this.$bus.$emit('async_done')
            findNotExistsOccupy().then(response => {
              this.hhqyBaseOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getHhqyOccupy(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
          getHhqyName(e) {
            console.log(e, this.hhqyBaseOptions)
            var hhqy = this.hhqyBaseOptions.find(item => item.id === e)
            this.form.actTitle = `合伙企业占用:${hhqy.name}`
          },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                hhqyId: undefined,
                dateInvestmentEstimate: undefined,
                reason: undefined,
                remark: undefined,
                createBy: undefined,
                updateBy: undefined,
                createTime: undefined,
                updateTime: undefined,
                delFlag: undefined,
                actId: undefined,
                actStatus: '0',
                actTitle: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function(type) {
                this.$refs['form'].validate(valid => {
                    // 验证数据
                    if (valid) {
                        // 弹出遮罩层
                        this.loading = true
                        // 保存数据
                        saveHhqyOccupy(this.form).then(response => {
                            if (response.code === 200) {
                                // 点击的按钮是保存并启动流程
                                if (type === 'process') {
                                    // 启动流程
                                    this.startProcess(response.data.id)
                                // 点击的仅保存
                                } else {
                                    this.msgSuccess(this.$t('保存成功'))
                                    // 关闭form页面
                                    this.closeForm()
                                }
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
            // 启动流程
           startProcess: function(dataId) {
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = this.form.actTitle
               // 业务表名
               processData.dataTable = 'hhqy_occupy'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/hhqy/occupy/hhqyOccupyForm'
               processData.viewPath = '@/views/hhqy/occupy/hhqyOccupyView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = this.menuKey
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           }

        }

    }
</script>
